package com.sghj.SysPermission.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sghj.SysPermission.entity.SysRole;
import com.sghj.SysPermission.entity.Vo.roleAddVo;
import com.sghj.SysPermission.service.SysRoleDeptService;
import com.sghj.SysPermission.service.SysRoleService;
import com.sghj.config.ReturnResult;
import com.sghj.config.handler.ResultCode;
import com.sghj.config.handler.exceptionHandler.MyException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xrx
 * @since 2022-02-18
 */
@RestController
@RequestMapping("/role")
public class SysRoleController {

    @Autowired
    private SysRoleService roleService;
    @Autowired
    private SysRoleDeptService roleDeptService;
    /**
     * 添加角色
     * @param role
     * @return
     */
    @RequiresPermissions("service:role:add")
    @PostMapping("addRole")
    public ReturnResult addRole(@Validated @RequestBody roleAddVo role){
        if (role==null){
            throw new MyException(ResultCode.PARAMETER,"缺少参数");
        }
        int row =roleService.addRole(role);
        if (row>0) {
            return ReturnResult.success().message("添加角色成功");
        }
        return ReturnResult.failed().message("添加角色失败");
    }

    /**
     * 根据id删除角色
     * @param rid
     * @return
     */
    @RequiresPermissions("service:role:remove")
    @GetMapping("removeRoleById/{rid}")
    public ReturnResult removeRoleById(@PathVariable Integer rid){
        if (rid==null){
            throw new MyException(ResultCode.PARAMETER,"缺少参数");
        }
        int b =roleService.removeRoleById(rid);

        if (b>0){
            return ReturnResult.success().message("删除角色成功");
        }
        return ReturnResult.failed().message("删除角色失败");
    }

    /**
     * 修改角色信息
     * @param role
     * @return
     */
    @RequiresPermissions("service:role:update")
    @PostMapping("updateRole")
    public ReturnResult updateRole(@Validated @RequestBody roleAddVo role){
        if (role==null||role.getSysRoleId()==null||roleService.getById(role.getSysRoleId())==null){
            throw new MyException(ResultCode.PARAMETER,"该角色不存在");
        }
        int b = roleService.updateRole(role);
        if (b>0){
            return ReturnResult.success().message("修改角色信息成功");
        }
        return ReturnResult.success().message("修改角色信息失败");
    }

    /**
     * 查询所有角色列表（除超管）
     * @return
     */
    @GetMapping("getRolesList")
    public ReturnResult getRolesList(){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.ne("role_key","admin");
        List list = roleService.list(wrapper);

        if (list!=null){
            return ReturnResult.success().data("RoleList",list).data("total",list.size());
        }
        return ReturnResult.failed().message("查询角色列表失败");
    }

    /**
     * 获取角色-权限关联信息
     * @return
     */
    @GetMapping("getRoleById/{rid}")
    public ReturnResult getRoleById(@PathVariable Integer rid){
        if (rid==null){
            throw new MyException(ResultCode.PARAMETER,"缺少参数");
        }

        roleAddVo role =roleService.getRoleById(rid);
        if (role!=null) {
            return ReturnResult.success().data("role", role);
        }
        return ReturnResult.failed().message("查询角色信息失败");
    }

    /**
     * 条件分页查询角色信息
     * @param current
     * @param size
     * @param roleVo
     * @return
     */
    @RequiresPermissions("service:role:list")
    @PostMapping("getRolePage/{current}/{size}")
    public ReturnResult getRolePage(@PathVariable long current, @PathVariable long size, @RequestBody(required = false) SysRole roleVo){
        if (current==0||size==0){
            throw new MyException(ResultCode.PARAMETER,"参数为空");
        }
        IPage<SysRole> page = new Page<>(current,size);
        IPage<SysRole> rolePage =roleService.getRolePage(roleVo,page);
        if (rolePage!=null) {
            return ReturnResult.success().data("total", rolePage.getTotal()).data("records", rolePage.getRecords());
        }
        return ReturnResult.failed().message("分页查询失败");
    }

    /**
     * 给角色分配权限
     * @param rid
     * @param menus
     * @return
     */
    @PostMapping("allocatMenus")
    public ReturnResult allocatMenus(Integer rid,int[] menus){
        if (rid==null||menus==null){
            throw new MyException(ResultCode.PARAMETER,"缺少参数");
        }
        if (roleService.getById(rid)==null){
            throw new MyException(ResultCode.NOTQUERIED,"该角色不存在");
        }
        boolean b =roleService.allocatMenus(rid,menus);
        if (b){
            return ReturnResult.success().message("分配权限成功");
        }
        return ReturnResult.failed().message("分配权限失败");
    }

    /**
     * 分配数据权限
     * @param
     * @return
     */
    @PostMapping("allocatDataScope")
    public ReturnResult allocatDataScope(@RequestBody SysRole role){
        if (roleService.getById(role.getSysRoleId())==null){
            throw new MyException(ResultCode.NOTQUERIED,"该角色不存在");
        }

        int b =roleService.allocatDataScope(role);
        if (b>0){
            return ReturnResult.success().message("分配数据权限成功");
        }
        return ReturnResult.failed().message("分配数据权限失败");
    }

    /**
     * 获取角色数据权限
     * @param rid
     * @return
     */
    @GetMapping("getRoleDataScope/{rid}")
    public ReturnResult getRoleDataScope(@PathVariable Integer rid){
        SysRole role = roleService.getById(rid);
        if (role==null){
            throw new MyException(ResultCode.NOTQUERIED,"该角色信息不存在");
        }
        //获取自定义部门信息
        int[] DataScopeList =roleDeptService.getRoleDeptList(rid);
        return ReturnResult.success().data("role",role).data("role",role).data("dataScope",DataScopeList);
    }

    /**
     * 更新数据权限
     * @param rid
     * @param dataScope
     * @return
     */
    @PostMapping("updateDataScope/{rid}/{dataScope}")
    public ReturnResult updateDataScope(@PathVariable Integer rid,@PathVariable Integer dataScope){
        if (rid==null||dataScope==null){
            throw new MyException(ResultCode.PARAMETER,"缺少参数");
        }
        SysRole role = roleService.getById(rid);
        if (role==null){
            throw new MyException(ResultCode.NOTQUERIED,"该角色信息不存在");
        }
        role.setDataScope(dataScope);
        boolean b = roleService.updateById(role);
        if (b){
            return ReturnResult.success().message("数据权限更新成功");
        }
        return ReturnResult.failed().message("数据权限更新失败");
    }
}

